package com.kms.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.kms.console.JSchConsole;
import com.kms.model.MGroup;
import com.kms.model.MSyspara;
import com.kms.util.CoreUtil;

public class TbsmonitorQuartz {
	SystemparameterServiceImpl sysparaService;

	public SystemparameterServiceImpl getSysparaService() {
		return sysparaService;
	}

	public void setSysparaService(SystemparameterServiceImpl sysparaService) {
		this.sysparaService = sysparaService;
	}
	
	
	public void executionQuartz(){
		List<MGroup> mgrouplist = sysparaService.selectAllMGroup();
		JSchConsole jc = new JSchConsole();
		for(MGroup mg:mgrouplist){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String datetime = sdf.format(new Date());
			String host = "";
			String user = "";
			String pwd = "";
			String dir = "";
			String dirctory = "";
			String commandstr ="";
			int port = 0;
			MSyspara mssysapp=new MSyspara();
			MSyspara mssysdb=new MSyspara();
			MGroup mgroup = mg;
				for(MSyspara sys:mgroup.getListsyspara()){
					if("APP".equals(sys.getDatatype())){
						mssysapp = sys;
					}else if("DB".equals(sys.getDatatype())){
						mssysdb = sys;
					}
				}
				
				//DB user
				if(mssysdb!=null){
					host = mssysdb.getOsip();
					user = mssysdb.getOsuser();
					pwd = mssysdb.getOspwd();
					dir = mssysdb.getDbzipdir();
					try {
						port = Integer.valueOf(mssysdb.getOsport());
					} catch (NumberFormatException e) {
					}
				}
				String ret="";
				try {
					//kms db, bond db
					List<String[]> infocommandlist = new ArrayList<String[]>();
					infocommandlist.add(new String[]{"kms"," cat .bash_profile"});
					infocommandlist.add(new String[]{"bond"," cat .bash_profile"});
					for(String[] infotype:infocommandlist){
						ret = ret + jc.sshExecute(host, user, pwd, infotype[1], port);
						String[] l = ret.split("\n");
						StringBuffer _s=new StringBuffer();
						for(String r:l){
							if(r.contains("export") || r.contains("DISPLAY") || r.contains("JAVA_HOME") || r.contains("CLASSPATH") || r.contains("PATH") ){
								_s.append(r);
								_s.append("\n");
							}
						}
						_s.append("expdp ");
						if("kms".equals(infotype[0])){
							_s.append(mssysdb.getKmsuser()+"/"+mssysdb.getKmspwd());
							_s.append("@"+mssysdb.getKmsinstance());
							_s.append(" directory="+mssysdb.getKmsdirectory());
						}else if("bond".equals(infotype[0])){
							_s.append(mssysdb.getBonduser()+"/"+mssysdb.getBondpwd());
							_s.append("@"+mssysdb.getBondinstance());
							_s.append(" directory="+mssysdb.getBonddirectory());
						}
						_s.append(" dumpfile="+infotype[0]+datetime+".dmp");
						_s.append(" logfile="+infotype[0]+datetime+".log");
						if("bond".equals(infotype[0])){
							_s.append(" rows=n ");
						}
						_s.append(" schemas="+infotype[0]+" ;");
//						_s.append("expdp kms/kms@kobra directory=dmpdir dumpfile=kms201108029.dmp logfile=kms201108029.log schemas=kms ;");					ret = ret + jc.sshExecute(host, user, pwd, _s.toString(), port);
						
						ret =ret+ jc.sshExecute(host, user, pwd, _s.toString(), port);
						
						if("kms".equals(infotype[0])){
							dirctory = mssysdb.getKmsdirectorypath();
							commandstr ="cd "+dirctory+" ; zip expdp_"+infotype[0]+datetime+".zip "+infotype[0]+datetime+".* ;";
							ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
							
							//scp /home/kms/kms_server_update.log root@192.168.8.10:/home/kms/monitor
							commandstr = "scp "+dirctory+"/expdp_kms"+datetime+".zip root@"+mssysapp.getTbsmonitorosip()+":"+mssysdb.getDbzipdir();
							ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
							
						}else if("bond".equals(infotype[0])){
							dirctory = mssysdb.getBonddirectorypath();
							commandstr ="cd "+dirctory+" ; zip expdp_"+infotype[0]+datetime+".zip "+infotype[0]+datetime+".* ;";
							ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
							
							commandstr = "scp "+dirctory+"/expdp_bond"+datetime+".zip root@"+mssysapp.getTbsmonitorosip()+":"+mssysdb.getDbzipdir();
							ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
						}
						
						commandstr = "cd "+dirctory+";rm *"+datetime+".*";
						try{
							port = Integer.valueOf(mssysdb.getOsport());
						}catch(Exception ex){}
						ret =ret+ jc.sshExecute(mssysdb.getOsip(), mssysdb.getOsuser(), mssysdb.getOspwd(), commandstr, port);
					}
					
					
					//backup log
					//app user
					host = mssysapp.getOsip();
					user = mssysapp.getOsuser();
					pwd = mssysapp.getOspwd();
					dir = mssysapp.getLogdir();
					try {
						port = Integer.valueOf(mssysdb.getOsport());
					} catch (NumberFormatException e) {
					}
					commandstr = "cd "+dir+";zip -r tbsserverlog"+datetime+".zip "+dir;
					ret =ret+ jc.sshExecute(host, user, pwd,commandstr, port);
					
					commandstr ="scp "+dir+"/tbsserverlog"+datetime+".zip root@"+mssysapp.getTbsmonitorosip()+":"+mssysapp.getLogzipdir();
					ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
					
					commandstr =" rm "+dir+"/*.zip; ";
					ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
				} catch (Exception e) {
					ret = e.toString();
					e.printStackTrace();
				}	
		}
	}
	
	public void connectTbsserver(){
		List<MGroup> mgrouplist = sysparaService.selectAllMGroup();
		String status = "";
		for(MGroup mg:mgrouplist){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String datetime = sdf.format(new Date());
			String date = sdf2.format(new Date());
			
			MSyspara mssysapp=new MSyspara();
			for(MSyspara sys:mg.getListsyspara()){
				if("APP".equals(sys.getDatatype())){
					mssysapp = sys;
				}
			}
			
			try {
				URL url = new URL(mssysapp.getMonitorserverurl());
				BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));  
				String s = "";  
				StringBuffer sb = new StringBuffer("");  
				while ((s = br.readLine()) != null) {  
					sb.append(s + "/r/n");  
				}  
				br.close();
				
				status =date+ "     |     alive\n";
			} catch (Exception e) {
				status =date+ "     |     error\n";
			} 
			CoreUtil.addContentToFile("d:/"+mg.getName()+"_"+datetime+"", status);
			//CoreUtil.addContentToFile(mssysapp.getMonitorlogdir()+"/"+mg.getName()+"_"+datetime+"", status);
		}
	}
}
